package com.demo1221.servlet;

import com.demo1221.demo02.servlet.BaseServlet;
import com.demo1221.entity.Person;
import com.demo1221.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

//@WebServlet("/demo1221/demo01.do")
public class TsetServlet01 extends BaseServlet {
    public void requestAttrs(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("requestAttr", "requestValue1111");
        req.setAttribute("requestAttr", "requestValue2222");
        req.removeAttribute("requestAttr");
    }

    public void sessionAttrs(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("SessionAttr", "SessionValue1111");
        req.getSession().setAttribute("SessionAttr", "SessionValue2222");
        req.getSession().removeAttribute("SessionAttr");
    }

    public void contextAttrs(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getServletContext().setAttribute("contextAttr", "contextValue1111");
        req.getServletContext().setAttribute("contextAttr", "contextValue2222");
        req.getServletContext().removeAttribute("contextAttr");
    }

    public void addPerson(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Person person = new Person(1,"zhangsan");
        req.getSession().setAttribute("person",person);
    }

    public void removePerson(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("person");
    }
    public void PassivateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("当前SessionId:" + req.getSession().getId());
        Student xiaoming = new Student(1, "xiaoming", 10);
        req.getSession().setAttribute("student",xiaoming);
    }

    public void ActivateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("当前SessionId:" + req.getSession().getId());
        Object student = req.getSession().getAttribute("student");
        System.out.println(student+""+new Date());
    }
}
